package com.lvcoding.codegen.utils;

import com.lvcoding.codegen.domain.Db;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * @Description 数据库连接工具类
 * @Date 2020-12-30 1:16 下午
 * @Author wuyanshen
 */
public class DbUtil {

    public static Connection connection;

    public static Connection getConnection() {
        return connection;
    }

    public static void disconnect() {
        connection = null;
    }

    public static Connection initDb(Db db) throws Exception{
        if(connection == null) {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection(db.getUrl(), db.getUsername(), db.getPassword());
            return connection;
        }
        return connection;
    }
}
